package com.xuetang9.BBS.contorller;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.xuetang9.BBS.domain.Customer;
import com.xuetang9.BBS.domain.Topic;
import com.xuetang9.BBS.service.CustomerService;
import com.xuetang9.BBS.service.SectionServic;
import com.xuetang9.BBS.service.TopicService;
import com.xuetang9.BBS.service.TopictypeService;


@Controller
public class CustomerController {

	@Autowired
	private CustomerService customerService;
	
	@Autowired
	private SectionServic sectionService ;
	
	@Autowired
	private TopicService topicService ;
	
	@Autowired
	private TopictypeService  topictypeService ;

	/**
	 * 访问登录界面
	 * @param map
	 * @return
	 */
	@RequestMapping(path = "/login", method = RequestMethod.GET)
	public String login(ModelMap map) {

		map.put("customers", new Customer());

		return "login";
	}
	
	/**
	 * 处理登录请求
	 * @param account
	 * @param password
	 * @param Session
	 * @return
	 */
	@RequestMapping(path = "/login", method = RequestMethod.POST)
	  public String login(String account ,String password, HttpSession Session) {
			
		Customer loginUser = customerService.login(account, password);
		
		if(loginUser==null){
	      	return "login";
		}
		//登录成功
		Session.setAttribute("login", loginUser);
	
		return "redirect:/Mune";
	
	}
	
	/**
	 * 访问注册界面
	 * 	 * @return
	 */
	@RequestMapping(path = "/regist", method = RequestMethod.GET)
	public String regist(ModelMap map) {
	    map.put("customer", new Customer());
		return "regist";
	}
	
	/**
	 * 处理注册请求
	 * @return
	 */
	@RequestMapping(path = "/regist", method = RequestMethod.POST)
	public String regist(ModelMap map,Customer customer) {
		map.put("customer", customer);
		
		return "redirect:/login";
	}
	
	
	
	/**
	 * 访问首页
	 * @param modelmap
	 * @param name
	 * @param top
	 * @return
	 */
	@RequestMapping(path = "/Mune",method = RequestMethod.GET)
	public String Mune(ModelMap modelmap ,String name){
		
		modelmap.put("sections", sectionService.getAllTopSection());
		modelmap.put("subSections", sectionService.getAllChildrenSection());
		modelmap.put("name", name);
		
		modelmap.put("topictype", topictypeService.findTopictype());
		
		return "Mune";
		
	}
	
	/**
	 * 访问贴吧页面
	 * @param modelmap
	 * @param name
	 * @param top
	 * @return
	 */
	@RequestMapping(path = "/post-bar",method = RequestMethod.GET)
	public String post(ModelMap modelmap ,String name){
		
		modelmap.put("sections", sectionService.getAllTopSection());
		modelmap.put("name", name);
		modelmap.put("topictype", topictypeService.findTopictype());
						
		return "post-bar";
		
	}
	
	/**
	 * 访问发帖页面
	 * @return
	 */
	@RequestMapping(path="/Sendpost", method=RequestMethod.GET )
	public String sendpost(ModelMap modelmap){
	
		modelmap.put("sections", sectionService.getAllTopSection());
		
		modelmap.put("topictype", topictypeService.findTopictype());
		
		
		
		return "Sendpost";
	}

	
	/**
	 * 处理发帖业务
	 * @return
	 */
	@RequestMapping(path="/Sendpost", method=RequestMethod.POST )
	public String dopost(ModelMap modelMap,Topic topic){
	
		modelMap.put("topictype", topictypeService.findTopictype());
		
		modelMap.put("topic", topicService.save(topic));
		
		
		return "post-bar";
		
		
	}
}
